<template>
    <div class="infinite-container" style="top:0;">
     <div class="visual-header">
    <mu-icon-button slot="left" @click="historyBack" style="position:absolute;">
      <i class="iconfont icon-back-copy t1"></i>
    </mu-icon-button>
    <div class="visual-title">提交预约</div>
    <select-role @liclick="selperson" ref="childRole"></select-role>
</div>
        <common-list title="预约科室" :desc="dept.deptName" ></common-list>
        <common-list title="预约医生" :desc="doctor.doctorName" v-show="!!doctor.doctorName"></common-list>
        <common-list title="预约日期" :desc="doctor.workDate" ></common-list>
        <common-list title="就诊预估时间" :desc="appointTime.startTime+'-'+appointTime.endTime"></common-list>
        <common-list title="就诊地点" :desc="doctor.address"></common-list>
        <common-list>
          <div slot="left">
            <div class="confirm-hospital">挂号费用</div>
          </div>
          <div class="confirm-money">
            {{doctor.charge | formatMoney}}
          </div>
        </common-list>
        <common-list title="就诊人" :desc="familyPerson.personName" margin="10" @click.native="changeRole"></common-list>
        <div class="text-info" style="text-align:left;">小提示: 点击就诊人可以切换就诊人哦</div>
        <div class="login pd20">
          <mu-raised-button @click="confirmClick" label="立即提交" fullWidth primary class="btn-login"></mu-raised-button>
          <div class="tips t3">
            <div class="tips-title">
              <i class="sprites bg_tips"></i>
              温馨提示
            </div>
            <ul>
              <li v-for="v in APPOINT_DESCRIPTION" :key="v">{{v}}</li>
            </ul>
          </div>
        </div>
        <mu-dialog :open="dialog" :title="dialogTitle">
          <div v-html="dialogContent" style="text-indent:2em;"></div>
          <div class="login center">
          <mu-raised-button class="btn-login" slot="actions" @click="goNext" primary label="确定"></mu-raised-button>
          </div>
        </mu-dialog>
    </div>
</template>

<script>
import {APPOINT_DESCRIPTION} from '../../common/js/message'
import CommonList from '../../components/CommonList'
import {commonAjax, ORG_ID, PLATFORM_PREFIX, CONFIRM_PAY_TIPS, PAY_URL, SUBMIT_SOURCE} from '../../api/api'
import SelectRole from '../../components/SelectRole'

export default {
  mounted () {
    this.$store.commit('UPDATE_HEADERSHOW')
    this.doctor.appointTime = localStorage.appointTime
  },
  filters: {
    formatMoney (value) {
      return '￥' + value.toFixed(2)
    }
  },
  data () {
    return {
      CONFIRM_PAY_TIPS: CONFIRM_PAY_TIPS,
      APPOINT_DESCRIPTION: APPOINT_DESCRIPTION,
      doctor: JSON.parse(localStorage.doctor),
      appointTime: JSON.parse(localStorage.appointTime),
      dialog: false,
      dialogTitle: '',
      dialogContent: '',
      familyPerson: JSON.parse(localStorage.familyPerson),
      dept: JSON.parse(localStorage.dept)
    }
  },
  methods: {
    historyBack () {
      history.back()
    },
    changeRole () {
      this.$refs.childRole.getFamilys()
    },
    selperson (val) {
      this.familyPerson = val
      localStorage.familyPerson = JSON.stringify(val)
    },
    goNext () {
      if (this.dialogTitle === '提示') {
        this.dialog = false
        window.history.back()
        return
      }
      // for ios pay
      if (localStorage.encCode) {
        this.$router.replace('/appointment/appoint-pay-thirdparty')
      } else {
        window.location.href = PAY_URL
      }
    },
    confirmClick () {
      this.$store.commit('UPDATE_LOADING')
      commonAjax(
        [{
          orgId: ORG_ID,
          mpiId: this.familyPerson.mpiId,
          domain: '',
          cardType: this.familyPerson.certificate.certificateType,
          cardNo: this.familyPerson.certificate.certificateNo,
          deptId: this.dept.deptId,
          localDeptId: this.dept.localDeptId,
          regDeptId: this.doctor.regDeptId,
          localRegDeptId: this.doctor.localRegDeptId,
          regDeptName: this.doctor.regDeptName,
          seqId: this.doctor.seqId,
          localDoctorId: '',
          doctorId: '',
          doctorName: this.doctor.doctorName,
          workDate: this.doctor.workDate,
          planTime: this.doctor.planTime,
          timeId: this.appointTime.timeId,
          startTime: this.appointTime.startTime,
          endTime: this.appointTime.endTime,
          register: '1',
          source: SUBMIT_SOURCE
        }, localStorage.appointType],
        `${PLATFORM_PREFIX}.registrationService`,
        'doRegistrationSync'
      ).then(res => {
        this.$store.commit('UPDATE_LOADING')
        if (res.data.code === 200) {
          localStorage.order = JSON.stringify(res.data.body)
          let d = res.data.body
          if (localStorage.appointType === '3') {
            this.dialogTitle = CONFIRM_PAY_TIPS[6].title
            this.dialogContent = CONFIRM_PAY_TIPS[6].content
            this.dialog = true
            return
          }
          if (d.daylight === '1') {
            // 夏令时
            this.dialogTitle = CONFIRM_PAY_TIPS[4].title
            this.dialogContent = CONFIRM_PAY_TIPS[4].content
          } else if (d.daylight === '2') {
            // 冬令时
            this.dialogTitle = CONFIRM_PAY_TIPS[5].title
            this.dialogContent = CONFIRM_PAY_TIPS[5].content
          } else {
            // 初诊
            if (d.firstDiagnosis === '1') {
              this.dialogTitle = CONFIRM_PAY_TIPS[0].title
              this.dialogContent = CONFIRM_PAY_TIPS[0].content
            } else {
              this.dialogTitle = CONFIRM_PAY_TIPS[d.nature].title
              this.dialogContent = CONFIRM_PAY_TIPS[d.nature].content
            }
          }
          this.dialog = true
        } else {
          this.dialogTitle = '提示'
          this.dialogContent = res.data.msg
          this.dialog = true
        }
      }).catch(res => {
        this.$store.commit('UPDATE_LOADING')
        console.warn(res)
      })
      // this.$router.push('/appointment/appoint-pay')
    },
    open () {
      this.dialog = true
    },
    close () {
      this.dialog = false
    }
  },
  beforeDestroy () {
    this.$store.commit('UPDATE_HEADERSHOW')
  },
  components: {
    CommonList, SelectRole
  }
}
</script>
<style lang="less">
@import '../../common/css/vars.less';
.tips{
  .tips-title {
    color:@color-primary;
    padding: 20px 0 10px;
    .bg_tips {
      display: inline-block;
      width: 27px;
      height: 16px;
      background-position: 0 -636px;
      background-size: 187px;
    }
  }
  li {
    font-size: 12px;
    line-height: 1.6em;
  }
}
</style>

